package com.fastloan.mgt.customer.service.impl;

import com.alibaba.fastjson.JSON;
import com.fastloan.common.BasicServiceImpl;
import com.fastloan.common.model.CheckResult;
import com.fastloan.mgt.application.base.model.ApplicationIPC;
import com.fastloan.mgt.application.base.service.ApplicationIPCService;
import com.fastloan.mgt.customer.dao.CustomerAmountMapper;
import com.fastloan.mgt.customer.service.CustomerAmountService;
import com.fastloan.mgt.customer.model.CustomerAmount;
import com.fastloan.mgt.customer.service.ZyCustomerService;
import com.fastloan.mgt.jx.model.vo.AppApproveVO;
import com.fastloan.mgt.product.basics.model.Product;
import com.fastloan.mgt.product.basics.service.ProductService;
import com.fastloan.safe.core.util.ManagerKit;
import com.kawins.result.ResultEnum;
import com.kawins.supers.table.manager.DataTable;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.fastloan.common.model.CheckResultEnum.*;
import static com.fastloan.mgt.customer.enums.CustomerAmountEnum.NORMALS;
import static com.fastloan.mgt.product.enums.ProductType.ADVANCE;
import static java.util.stream.Collectors.toList;

/**
 * @author admins
 * createTime 2018/01/05
 */
@org.springframework.stereotype.Service
public class CustomerAmountServiceImpl extends BasicServiceImpl<CustomerAmount> implements CustomerAmountService {
    @org.springframework.beans.factory.annotation.Autowired
    private CustomerAmountMapper customerAmountMapper;

    @Autowired
    private ApplicationIPCService applicationIPCService;

    @Autowired
    private ZyCustomerService customerService;

    @Autowired
    private ProductService productService;

    @Override
    @DataTable(name = "jxApprove")
    public List<AppApproveVO> queryApprove(Map<String, Object> map) {
        map.put("orgId", ManagerKit.getOrgId());
        List<AppApproveVO> appApproveVOS = customerAmountMapper.queryApprove(map);
        return appApproveVOS.stream()
                .peek(appApproveVO -> {
                    List<ApplicationIPC> applicationIPCS = applicationIPCService.selectApplicationIPCNoTree(appApproveVO.getApplicationId());
                    appApproveVO.setIpcValue(JSON.toJSONString(applicationIPCS));
                }).collect(toList());
    }

    @Override
    public AppApproveVO queryApproveInfo(String applicationId) {
        return customerAmountMapper.queryApproveInfo(applicationId);
    }

    @Override
    public CheckResult creditVerify(String customerId, String productId) {
        Map<String, Object> map = new HashMap<>();
        Product product = productService.selectByPrimaryKey(productId);
        if (product.getWhetherCredit() != null && product.getWhetherCredit().equals(1)) {
            CustomerAmount customerAmount = new CustomerAmount();
            customerAmount.setStatus(NORMALS.getCode());
            customerAmount.setCustomerId(customerId);
            customerAmount = customerAmountMapper.selectOne(customerAmount);
            //验证客户是否预授信
            if (customerAmount == null) {
                Product productTemp = new Product();
                productTemp.setOrgId(product.getOrgId());
                productTemp.setProductType(ADVANCE.getType());
                productTemp = productService.selectOne(productTemp);
                if (productTemp == null) {
                    log.info("未查询到对应产品");
                    return CheckResult.fail("未查询到对应产品", ILLEGAL_REQUEST.getCode());
                }
                map.put("false", productTemp.getProductId());
                return CheckResult.success(map);
            }
        } else if (product.getWhetherCredit().equals(2)) {
            return CheckResult.fail(CUSTOMER_INFO_ALREADY_CREDIT.getMsg(), CUSTOMER_INFO_ALREADY_CREDIT.getCode());
        }
        map.put("true", productId);
        return CheckResult.success(map);
    }

    @Override
    public Integer queryIfCredit(String customerId) {
        return customerAmountMapper.queryIfCredit(customerId);
    }

    @Override
    public CheckResult verifyApplyAmount(String customerId, String productId, BigDecimal applyAmount) {
        Product product = productService.selectByPrimaryKey(productId);
        if (product.getWhetherCredit() != null && product.getWhetherCredit().equals(1)) {
            CustomerAmount customerAmount = new CustomerAmount();
            customerAmount.setStatus(NORMALS.getCode());
            customerAmount.setCustomerId(customerId);
            customerAmount = customerAmountMapper.selectOne(customerAmount);
            if (applyAmount.compareTo(customerAmount.getCreditAmount()) > 0) {
                return CheckResult.fail("信用额度不足, 无法申请此产品", ResultEnum.OPERATION_FAILED.getCode());
            }
        }
        return CheckResult.success();
    }

    @Override
    public Integer changeCreditAmount(String applicationId, BigDecimal applyAmount) {
        CustomerAmount customerAmount = new CustomerAmount();
        customerAmount.setApplicationId(applicationId);
        customerAmount.setStatus(1);
        CustomerAmount amount = customerAmountMapper.selectOne(customerAmount);
        amount.setCreditAmount(amount.getCreditAmount().subtract(applyAmount));
        return updateByPrimaryKeySelective(amount);
    }
}
